//
//  CUploadEditView.m
//  HCHK_iphone
//
//  Created by yilong zhang on 12-3-14.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CUploadEditView.h"

@implementation CUploadEditView

@synthesize delegate;
@synthesize m_Caption;
@synthesize m_FoodName;
@synthesize m_ShareTable;
@synthesize m_Sina;
@synthesize m_Tx;
@synthesize m_bIsTx;
@synthesize m_bIsSina;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        bg.image = [UIImage imageNamed:@"appbg.jpg"];
        [self addSubview:bg];
        [bg release];
        
        UINavigationBar *toolbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 44)]; 
        UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 49)];
        view.image = [UIImage imageNamed: @"barbg.png"];
        [toolbar addSubview:view];
        [view release];
        
        UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"好吃客"];
        [toolbar pushNavigationItem:item animated:NO];
        [item release];
        
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 32)];
        title.textAlignment = UITextAlignmentCenter;
        title.backgroundColor = [UIColor clearColor];
        title.font = [UIFont boldSystemFontOfSize:18];
        title.text = @"好吃客";
        item.titleView = title;
        [title release];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 60, 32);
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
        [btn setTitle:@"返回" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(ReturnBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        item.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
        
        UIView *m_Seg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 90, 31)];
        
        btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 45, 31);
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setBackgroundImage:[UIImage imageNamed:@"delious.png"] forState:UIControlStateNormal];
        [btn setTitle:@"保存" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        btn.tag = 0;
        [btn addTarget:self action:@selector(SegClicked:) forControlEvents:UIControlEventTouchUpInside];
        [m_Seg addSubview:btn];
        
        btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(45, 0, 45, 31);
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setBackgroundImage:[UIImage imageNamed:@"want.png"] forState:UIControlStateNormal];
        [btn setTitle:@"上传" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        btn.tag = 1;
        [btn addTarget:self action:@selector(SegClicked:) forControlEvents:UIControlEventTouchUpInside];
        [m_Seg addSubview:btn];

        item.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:m_Seg] autorelease];
        [m_Seg release];
        
        toolbar.barStyle = UIBarStyleBlackOpaque;
        [self addSubview:toolbar];
        [toolbar release];
        [toolbar drawRect:toolbar.frame];
        
        m_FoodName = [[UILabel alloc] initWithFrame:CGRectMake(15, 45, frame.size.width-30, 60)];
        m_FoodName.backgroundColor = [UIColor clearColor];
        m_FoodName.text = @"美食名称";
        m_FoodName.font = [UIFont fontWithName:@"Helvetica" size:16];
        m_FoodName.numberOfLines = 2;
        m_FoodName.textColor = [UIColor colorWithRed:0.233 green:0.184 blue:0.133 alpha:1];
        [self addSubview:m_FoodName];
        [m_FoodName release];
        
        m_Caption = [[UITextView alloc] initWithFrame:CGRectMake(15, 104, frame.size.width-30, 120)];
        m_Caption.scrollEnabled = NO;
        m_Caption.text = @"想说些什么...";
        m_Caption.returnKeyType = UIReturnKeyDone;
        m_Caption.layer.borderWidth = 1;
        m_Caption.layer.cornerRadius= 8;
        m_Caption.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        m_Caption.textColor = [UIColor grayColor];
        m_Caption.delegate = self;
        [self addSubview:m_Caption];
        [m_Caption release];
        
        m_ShareTable = [[UITableView alloc] initWithFrame:CGRectMake(15, 244, frame.size.width-30, 100)];
        m_ShareTable.layer.cornerRadius = 8;
        m_ShareTable.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        m_ShareTable.layer.borderWidth = 1;
        m_ShareTable.scrollEnabled = NO;
        m_ShareTable.delegate = self;
        m_ShareTable.dataSource=self;
        [self addSubview:m_ShareTable];
        [m_ShareTable release];
        
        m_bIsTx = NO;
        m_bIsSina=NO;
    }
    return self;
}

//-(void)SegClicked:(UISegmentedControl*)seg
-(void)SegClicked:(UIButton*)seg
{
    [m_Caption resignFirstResponder];
    
    //drop
    if (1 == seg.tag)
    {
        if ([m_Caption.text length] > 0)
        {
            [delegate DidUploadEditViewActived:self Command:1 Value1:nil Value2:nil];
        }
        else 
        {
            UIAlertView *none = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不说啥就上传吗?" delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否", nil];
            [none show];
            [none autorelease];
        }
    }
    //save
    else 
    {
        [delegate DidUploadEditViewActived:self Command:0 Value1:nil Value2:nil];
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //yes
    if (0 == buttonIndex)
    {
        m_Caption.text = @"想说些什么...";
        [delegate DidUploadEditViewActived:self Command:1 Value1:nil Value2:nil];
    }
    //no
    else 
    {
        ;//
    }
}

-(void)ReturnBtnClicked
{
    [m_Caption resignFirstResponder];
    [delegate DidUploadEditViewActived:self Command:2 Value1:nil Value2:nil];
}

- (void)textViewDidChange:(UITextView *)textView
{
    if ([textView.text length] > 140)
    {
        textView.text = [textView.text substringToIndex:140];
    }
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if ([m_Caption.text isEqualToString:@"想说些什么..."])
    {
        textView.text = nil;
    }
    
    return YES;
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        return NO;
    }
    
    return YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = NO;
    
    //sina
    if (0 == indexPath.row)
    {
        if (m_bIsSina)
        {
            m_bIsSina = NO;
            m_Sina.text = @"断开";
        }
        else 
        {
            if (1 == [[CUser DefaultUser].U_SinaOn intValue])
            {
                m_bIsSina = YES;
                m_Sina.text = @"连接";
            }
            else 
            {
                UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"新浪微博分享快关未打开,在设置打开." delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
                [alter show];
                [alter autorelease];
            }
        }
    }
    //tx
    else if (1 == indexPath.row)
    {
        if (m_bIsTx)
        {
            m_bIsTx = NO;
            m_Tx.text = @"断开";
        }
        else 
        {
            if (1 == [[CUser DefaultUser].U_QqOn intValue])
            {
                m_bIsTx = YES;
                m_Tx.text = @"连接";
            }
            else 
            {
                UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"腾讯微博分享快关未打开,在设置打开." delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil];
                [alter show];
                [alter autorelease];
            }
        }
    }
    else {;}
}

-(void)ListenSinaSwitch:(NSNotification*)notification
{
    NSNumber *ret = notification.object;
    
    if ([ret boolValue])
    {
        m_bIsSina = YES;
        m_Sina.text = @"连接";
    }
    else 
    {
        m_bIsSina = NO;
        m_Sina.text = @"断开";
    }
    
    [[CRootCtrlManager DefaultRootCtrl] BringCtrlToFront:1];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MSG_SINA_SWITCH object:nil];
}

-(void)ListenTxSwitch:(NSNotification*)notification
{
    NSNumber *ret = notification.object;
    
    if ([ret boolValue])
    {
        m_bIsTx = YES;
        m_Tx.text = @"连接";
    }
    else 
    {
        m_bIsTx = NO;
        m_Tx.text = @"断开";
    }
    
    [[CRootCtrlManager DefaultRootCtrl] BringCtrlToFront:1];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MSG_QQ_SWITCH object:nil];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCellStyle style = UITableViewCellStyleSubtitle;
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BaseCell"];
	
	if (nil == cell)
	{
		cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:@"BaseCell"] autorelease];
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        cell.textLabel.textColor = [UIColor grayColor];
        cell.textLabel.text = @"Share on";
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
        
        cell.detailTextLabel.textColor = [UIColor blackColor];
        cell.detailTextLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
        
        //cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    //sina
    if (0 == indexPath.row)
    {
        if (!self.m_Sina)
        {
            self.m_Sina = [[[UILabel alloc] initWithFrame:CGRectMake(160, 10, 100, 26)] autorelease];
            self.m_Sina.text = @"断开";
            self.m_Sina.font = [UIFont fontWithName:@"Helvetica" size:15];
            self.m_Sina.textAlignment = UITextAlignmentRight;
            [cell addSubview:self.m_Sina];
            //[self.m_Sina release];
        }
        cell.imageView.image = [UIImage imageNamed:@"sina.png"];
        cell.detailTextLabel.text = @"新浪微博";
    }
    //qq
    else if (1 == indexPath.row)
    {
        if (!self.m_Tx)
        {
            self.m_Tx = [[[UILabel alloc] initWithFrame:CGRectMake(160, 10, 100, 26)] autorelease];
            self.m_Tx.text = @"断开";
            self.m_Tx.font = [UIFont fontWithName:@"Helvetica" size:15];
            self.m_Tx.textAlignment = UITextAlignmentRight;
            [cell addSubview:self.m_Tx];
            //[self.m_Tx release];
        }
        cell.imageView.image = [UIImage imageNamed:@"tx.png"];
        cell.detailTextLabel.text = @"腾讯微博";
    }
    else {;}
    
    return cell;
}

@end
